package com.wn.class30.service.impl;

import com.wn.class30.entity.*;
import com.wn.class30.mapper.MealDao;
import com.wn.class30.mapper.UserCuponDao;
import com.wn.class30.mapper.UserDao;
import com.wn.class30.mapper.VipDao;
import com.wn.class30.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class UserServiceImp implements UserService {
    @Resource
    private UserDao userDao;
    @Resource
    private VipDao vipDao;
    @Resource
    private UserCuponDao userCuponDao;
    @Resource
    private MealDao mealDao;

    @Override
    /*用户关注公众号*/
    public void insertUser(User user) throws Exception {
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andOpenidEqualTo(user.getOpenid());
        List<User> users = userDao.selectByExample(userExample);
        log.debug("users="+String.valueOf(users));
        if(users==null || users.size()==0){
            int insert = userDao.insertSelective(user);
            log.debug("关注成功");
        }else{
            user.setUserId(users.get(0).getUserId());
            userDao.updateByPrimaryKeySelective(user);
            log.debug("重新关注成功");
        }
    }
    /*用户取关公众号*/
    @Override
    public void updateUser(User user) throws Exception {
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andOpenidEqualTo(user.getOpenid());
        List<User> users = userDao.selectByExample(userExample);
        if(users==null || users.size()==0){
            log.debug("数据库数据丢失");
        }else{
        user.setUserId(users.get(0).getUserId());
        userDao.updateByPrimaryKeySelective(user);}
    }

    @Override
    public List<User> queryByOpenid(String openid) throws Exception {
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andOpenidEqualTo(openid);
        List<User> users = userDao.selectByExample(userExample);
        return users;
    }

    @Override
    public Vip queryLvNameByUserid(Integer userid) throws Exception {
        User user = userDao.selectByPrimaryKey(userid);
        Vip vip = vipDao.selectByPrimaryKey(user.getLvId());
        return vip;
    }

    @Override
    public List<UserCupon> queryUserCuponByUserid(Integer userid) throws Exception {
        UserCuponExample userCuponExample = new UserCuponExample();
        UserCuponExample.Criteria criteria = userCuponExample.createCriteria();
        criteria.andUserIdEqualTo(userid);
        List<UserCupon> userCupons = userCuponDao.selectByExample(userCuponExample);
        return userCupons;
    }
    //查询所有vip套餐
    @Override
    public List<Meal> queryMeal() {
        return mealDao.selectByExample(null);
    }
    //根据用户id查询用户信息
    @Override
    public User queryUserByUderid(Integer userId) {
        return userDao.selectByPrimaryKey(userId);
    }
    //查询所有vip等级

    @Override
    public List<Vip> queryAllVip() {
        return vipDao.selectByExample(null);
    }

    //查询用户和其对应的优惠券,三表联立
    @Override
    public List<Map<String, Object>> queryUserCupon2(Integer userid) {
        List<Map<String, Object>> maps = userDao.selectMap(userid);
        return maps;
    }
}
